<?php
$query_rsListar = $_POST['query_rsListar'];	
$programa = $_POST['programa'];	
	
$columnas = $_POST['columnas'];	
$columnas = split(',', $columnas);


$lis = $_POST['lis'];	
$fil = $_POST['fil'];	
$p = $_POST['p'];

$est = $_POST['est'];	
$req = $_POST['req'];	
$num = $_POST['num'];	
$let = $_POST['let'];	

$Eti = $_POST['Eti'];	
$db = $_POST['db'];	
$ht = $_POST['html'];	
$Sel = $_POST['Sel'];



$lis = split('/', $lis);
$fil = split('/', $fil);
$p = split('/', $p);

$est = split('/', $est);
$req = split('/', $req);
$num = split('/', $num);
$let = split('/', $let);

$Eti = split('/', $Eti);
$db = split('/', $db);
$ht = split('/', $ht);
$Sel = split('/', $Sel);
//print_r($lis);

//Llave primaria
$identificador = '';
foreach($p as $index=>$valor){
	if($valor == 'true'){
		$identificador = $columnas[$index];
	}
}

//┌────────────────────────────────┐Crear el filtro del select


$count = 0;
foreach($fil as $valor){
	if($valor == 'true'){
		$count++;	
	}
}

$sql_filtro = '';
$sql_0 = $query_rsListar;

if($count == 0){
	$sql_filtro = $sql_0;
}else{
	
	$order = (strripos($sql_0, 'order') 	> 0 ? strripos($sql_0, 'order') : 0);
	$group =  (strripos($sql_0, 'group') 	> 0 ? strripos($sql_0, 'group') : 0);
	$having = (strripos($sql_0, 'having') 	> 0 ? strripos($sql_0, 'having') : 0);
	$limit = (strripos($sql_0, 'limit') 	> 0 ? strripos($sql_0, 'limit') : 0);
	
	$vector = array($order, $group, $having, $limit);
	
	if(min($vector)==0 and max($vector)==0){// NO existe ninguno de estos: order, group, having, limit 
		
		 if(strripos($sql_0, 'where') == '' ){
			$sql_filtro = $sql_0."\nWHERE \".\$_GET['filtroCampo'].\" LIKE '%\".\$vfiltro.\"%'";	//  \n salto de linea			
		 }else if(strripos($sql_0, 'where') > 0){
			$sql_filtro = $sql_0."\nAND \".\$_GET['filtroCampo'].\" LIKE '%\".\$vfiltro.\"%'"; //  \n salto de linea			
		 }
		 		 
	}
	
	// SI existe alguno de estos: order, group, having, limit 
	$posiciones = array();
	if($order != 0){
		array_push($posiciones, $order);
	}		
	if($group != 0){
		array_push($posiciones, $group);
	}		
	if($having != 0){
		array_push($posiciones, $having);
	}		
	if($limit != 0){
		array_push($posiciones, $limit);
	}		

	if(count($posiciones) > 0){
		
		$minimo = min($posiciones);
		$antes_sql = substr ( $sql_0 , 0, $minimo );	
		$despues_sql = substr ( $sql_0 , $minimo );				

		 if(strripos($sql_0, 'where') == '' ){
			$sql_filtro = $antes_sql."WHERE \".\$_GET['filtroCampo'].\" LIKE '%\".\$vfiltro.\"%'\n".$despues_sql;	//  \n salto de linea
		 }else if(strripos($sql_0, 'where') > 0){
			$sql_filtro = $antes_sql."AND \".\$_GET['filtroCampo'].\" LIKE '%\".\$vfiltro.\"%'\n".$despues_sql;		//  \n salto de linea
		 }		

	}
}
//echo $sql_filtro;

//└────────────────────────────────┘

$html = "";
$html = "<?php 
/* 
Proyecto: Sistema Victoria 2.0
Programador: Felipe Guerra
Año: 2015
Flores de tumbabiro ©
*/
require_once('../core/libreria.php');
require_once('../db/connect.php'); 

\$np = '$programa';
\$cp = \$_GET['cp'];
 
//┌───────────────────────────────────────────────────────────────────────┐
\$maxRows_rsListar = ((isset(\$_GET['maxRows_rsListar'])) ? \$_GET['maxRows_rsListar'] : \$_SESSION['NUM_REGISTROS']);

\$pageNum_rsListar = 0;
if (isset(\$_GET['pageNum_rsListar'])) {
	\$pageNum_rsListar = ((isset(\$_GET['pageNum_rsListar'])) ? (\$_GET['pageNum_rsListar'] - 1) : 0);
}
\$startRow_rsListar = \$pageNum_rsListar * \$maxRows_rsListar;

//--------------------SELECT INICIO---------------------
mysql_select_db(\$database_victoria, \$victoria );
\$query_rsListar = \"
".$query_rsListar."
\";
//--------------------SELECT FIN------------------------

//====================FILTRO INICIO=====================	
if (isset(\$_GET['filtro']) and \$_GET['filtro']!=''){
				
	\$vfiltro =  mysql_real_escape_string(\$_GET['filtro']);
	\$query_rsListar = \"
".$sql_filtro."
	\";
	\$startRow_rsListar = 0;
	\$pageNum_rsListar = 0;
	\$maxRows_rsListar = 999999999;
}
//====================FILTRO FIN========================	

\$query_limit_rsListar = sprintf(\"%s LIMIT %d, %d\", \$query_rsListar, \$startRow_rsListar, \$maxRows_rsListar);
\$rsListar = mysql_query(\$query_limit_rsListar, \$victoria ) or die(mysql_error());
\$rsNum = mysql_num_rows(\$rsListar);

//....................PAGINACION INICIO.................
if (isset(\$_GET['totalRows_rsListar'])) {
	\$totalRows_rsListar = \$_GET['totalRows_rsListar'];
} else {
	\$all_rsListar = mysql_query(\$query_rsListar);
	\$totalRows_rsListar = mysql_num_rows(\$all_rsListar);
}
\$totalPages_rsListar = ceil(\$totalRows_rsListar/\$maxRows_rsListar);
//....................PAGINACION FIN....................

//└───────────────────────────────────────────────────────────────────────┘
?>    

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<link rel=\"stylesheet\" href=\"../css/list.css\" /><!--Importante, siempre debe ir antes del css del menu vertical-->
<link href=\"../css/dcverticalmegamenu.css\" rel=\"stylesheet\" type=\"text/css\" /><!--	Menu vertical	-->
<script src=\"../js/jquery.js\"></script>
<link rel=\"stylesheet\" href=\"../css/jquery-ui.css\" />
<script src=\"../js/jquery-ui.js\"></script>
</head>
<body id=\"bodyIndex\" >

<form action=\"\" method=\"get\" name=\"forma\" id=\"forma\" >    
   
<table align=\"center\" width=\"90%\" border=\"1\" style=\"background-color:#FFFFFF\">
  <tr>
    <td class=\"cabecera_1_td\" >
	    <img style=\"position: absolute; top: 8px;\"  src=\"../imagenes/listABC/application_form.png\" />
        <strong style=\"color:#999; margin-left:25px\"><?php echo programaNom(\$cp)?></strong>	
	</td>
  </tr>
  <tr class=\"cabecera_2_tr\">
    <td class=\"cabecera_2_td\">
	    <?php require_once('../core/listCabecera.php'); ?>
    </td>
  </tr>
  <tr>
    <td style=\"height: <?php echo \$_SESSION['td'];?>px;\">
    
    <!------------INICIO - LIST----------->
    <div style=\"overflow-y:scroll; height: 100%;\">
	
        <table width=\"100%\" border=\"0\" class=\"list_css\" >
          <tr class=\"list_cabecera\">
			";
		
	foreach($lis as $index=>$valor){
		if($valor == 'true'){		
			$asterisco = '';
			if($fil[$index] == 'true'){
				$asterisco = '*';
			}
			
			$html .= "<td>".$asterisco.$Eti[$index]."</td>
			";		
		}
	}

	foreach($est as $index=>$valor){
		if($valor == 'true'){
			$html .= "<td>*".$Eti[$index]."</td>
			";				
		}
	}

$html .= "<td class=\"acciones_css <?php echo \$ocultar?>\">Acciones</td>
          </tr>
		<?php   
		while(\$row = mysql_fetch_assoc(\$rsListar)){
		?>          
          <tr class=\"tr_css\">
            ";
		  
	foreach($lis as $index=>$valor){
		if($valor == 'true'){		
			$html .= "<td><?php echo \$row['".trim($columnas[$index])."'];?></td>
			";		
		}
	}

	foreach($est as $index=>$valor){
		if($valor == 'true'){
			$html .= "<td><img src=\"../imagenes/listABC/<?php echo (\$row['".trim($columnas[$index])."'] == 1 ? 'check' : 'mail-delete' );?>.png\" width=\"12\" height=\"12\" /></td>";				
		}
	}		  
		  
$html .= "
            <td class=\"<?php echo \$ocultar?>\" >
	            <?php \$id = \$row['".trim($identificador)."'];
                if(\$eliminar == 1){ ?>
                <a href=\"<?php echo \$np.'B.php' ?>?id=<?php echo \$id;?>&np=<?php echo \$np;?>&cp=<?php echo \$cp;?>\" title=\"Eliminar registro\" class=\"delete-icon\" ></a>                
                <?php }
				if(\$editar == 1){ ?>			
                <a href=\"<?php echo \$np.'C.php' ?>?id=<?php echo \$id;?>&np=<?php echo \$np;?>&cp=<?php echo \$cp;?>\" title=\"Editar registro\" class=\"edit-icon\" ></a>
                <?php }
				if(\$ver == 1){  ?>				
                <a href=\"<?php echo \$np.'Ver.php' ?>?id=<?php echo \$id;?>&np=<?php echo \$np;?>&cp=<?php echo \$cp;?>\" title=\"Ver registro\" class=\"read-icon\" ></a>               
                <?php } ?>  				
            </td>  
          </tr>
		<?php
		}
		?>
        </table>
    
    </div>	
    <!------------FIN - LIST--------------> 
      
    </td>
  </tr>
  <tr class=\"pie_1_tr\">
    <td class=\"pie_1_td\">
        Filtro:
        <input id=\"filtro\" name=\"filtro\" class=\"validar\" type=\"text\" value=\"<?php echo \$_GET['filtro'];?>\" maxlength=\"50\" style=\"border: 0 outset;\" />
        <select name=\"filtroCampo\" id=\"filtroCampo\" class=\"validar\">
            <option value=\"\" >Seleccione</option>";
			
	foreach($fil as $index=>$valor){
		if($valor == 'true'){		
			$html .= "
            <option value=\"".trim($columnas[$index])."\" >".trim($Eti[$index])."</option>";	
		}
	}			
			
$html .= "                       
        </select>
        <input type=\"button\" value=\"Filtrar\"  class=\"btn btn-large\" onclick=\"validaRegistro(\$('#forma'))\"/>
        <input name=\"mostrar_todos\" id=\"mostrar_todos\" type=\"button\" value=\"Mostrar todos\"  class=\"btn btn-large\"/>
    </td>
  </tr>   
  
  <tr>
    <td>
	    <?php require_once('../core/listPie.php'); ?>
    </td>
  </tr>
</table>

<input name=\"cp\" id=\"cp\" type=\"hidden\" value=\"<?php echo \$cp?>\" />

<form>

</body>
</html>";
    $nuevoarchivo = fopen($programa.'List.php', "w+");	
    fwrite($nuevoarchivo,$html);
    fclose($nuevoarchivo);
	//echo '<br>FIN';


?> 
